import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        userInfo: null,
        apiField: {
            module: 'member',
            api: 'GetLoginUser',
            data: 'userInfo',
            search: {}
        },
        defaultFace: '/assets/images/avatar.png',
        nameDialogShow: false,
        nameDialogButtons: [{ text: '取消' }, { text: '确定' }],
        name: null
    },
    async onLoad() {
        this.data.wxLoginRes = await wx.$Fun.wxLogin()
        this.fetchData()
    },
    async fetchData() {
        this.handleFetchData({})
    },
    async getPhoneNumber(e) {
        if (e.detail.errMsg !== 'getPhoneNumber:ok') return
        try {
            await wx.$Api.member.UpdatePhone({
                data: {
                    WxCode: this.data.wxLoginRes.code,
                    IV: e.detail.iv,
                    EncryptedData: e.detail.encryptedData
                }
            })

            this.fetchData()
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
        finally {
            this.data.wxLoginRes = await wx.$Fun.wxLogin()
        }
    },
    chooseImage() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: async (res) => {
                let invalidIndex = -1
                res.tempFiles.forEach((item, index) => {
                    if (item.size > this.data.maxSize) {
                        invalidIndex = index
                    }
                })
                if (invalidIndex >= 0) {
                    return
                }
                this.uploadImage(res)
            },
            fail: (fail) => { }
        })
    },
    async uploadImage(file) {
        try {
            // 获取文件内容
            let imgBase64 = wx.getFileSystemManager().readFileSync(file.tempFilePaths[0], "base64")
            const res = await wx.$Api.face.Check({
                data: {
                    ExcludeId: 'true',
                    FaceBase64String: imgBase64
                },
                hasLoading: true
            })

            await wx.$Api.member.UpdateInfo({
                data: {
                    Name: this.data.userInfo.Name,
                    FaceBase64String: 'data:image/jpg;base64,' + res.Data.FaceBase64String,
                    FaceFeature: res.Data.FaceFeature
                },
                hasLoading: true
            })

            this.updateNeedRefresh(true)
            this.fetchData()
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }

    },
    handleShowNameDialog(e) {
        this.setData({
            nameDialogShow: true,
            name: this.data.userInfo.Name
        })
    },
    tapNameDialogButton(e) {
        if (e.detail.index == 0) {
            this.setData({
                nameDialogShow: false
            })
        }
        else {
            //保存
            this.updateName()
        }
    },
    handleNameChange(e) {
        this.setData({
            name: e.detail.value
        })
    },
    async updateName() {
        if (!this.data.name) {
            wx.$Fun.showToast('姓名不能为空')
            return
        }
        try {
            await wx.$Api.member.UpdateInfo({
                data: {
                    Name: this.data.name
                }
            })
            this.setData({
                nameDialogShow: false
            })
            this.updateNeedRefresh(true)
            this.fetchData()
        }
        catch (e) {

        }
    },
    handleLogout() {
        wx.$Fun.logout()
    },
    handlePreview(e) {
        var url = e.currentTarget.dataset.url
        if (url) {
            wx.previewImage({
                urls: [url],
            })
        }
    },
}))